using AutoMapper;
using Ellwood.Manage.Business.Interface;
using Ellwood.Manage.Common.Core.result;
using Ellwood.Manage.Mapper.EntityDto;
using Ellwood.Manage.MentApi.Utility.Filters;
using Ellwood.Manage.MentApi.Utility.InitDatabaseExt;
using Ellwood.Manage.MentApi.Utility.SwaggerExt;
using Ellwood.Manage.Models.Entity;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Ellwood.Manage.MentApi.Controllers.SystemApi;

/// <summary>
/// 用户管理控制器
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
[CustomAsyncExceptionFilter]
[ApiExplorerSettings(IgnoreApi = false,GroupName = nameof(ApiVersion.V1))] // 指定版本
[Function(MuType.Page,"用户管理")]
public class UserManageController:ControllerBase
{
    
    private readonly ILogger<UserManageController> _logger;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="logger"></param>
    public UserManageController(ILogger<UserManageController> logger)
    {
        _logger = logger;
    }
    
    /// <summary>
    /// 用户信息-分页查询
    /// </summary>
    /// <param name="userManagerService"></param>
    /// <param name="mapper"></param>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <param name="searchaString"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("{pageIndex:int}/{pageSize:int}/{searchaString?}")]
    [Route("{pageIndex:int}/{pageSize:int}")]
    [Function(MuType.Btn,"用户信息-分页查询")]
    public async Task<JsonResult> GetUserPage([FromServices] IUserManagerService userManagerService, 
        [FromServices] IMapper mapper, int pageIndex,int pageSize,string? searchaString=null)
    {
        Expressionable<SysUser> expressionable=new Expressionable<SysUser>();

        expressionable.AndIF(!string.IsNullOrWhiteSpace(searchaString), u => u.UserName.Contains(searchaString));

        PagingData<SysUser> paging = userManagerService.QueryPage<SysUser>(
            expressionable.ToExpression(), pageSize, pageIndex, c => c.UserId, false);
        
        ApiDataResult<PagingData<SysUserDto>> result = new ApiDataResult<PagingData<SysUserDto>>()
        {
            Code=200,
            Success = true,
            Message = "用户分页查询",
            Data = mapper.Map<PagingData<SysUser>,PagingData<SysUserDto>>(paging),
        };

        return await Task.FromResult(new JsonResult(result));
    }

    /// <summary>
    /// 添加用户
    /// </summary>
    /// <param name="userManagerService"></param>
    /// <param name="mapper"></param>
    /// <param name="userDto"></param>
    /// <returns></returns>
    [HttpPost]
    [CustomAsyncValidateParaActionFilter]
    [Function(MuType.Btn,"用户信息-用户添加")]
    public async Task<JsonResult> AddUser([FromServices] IUserManagerService userManagerService,
        [FromServices] IMapper mapper,SysUserDto userDto)
    {
        SysUser addUser = mapper.Map<SysUserDto, SysUser>(userDto);
        
        // 密码加密 todo
        userManagerService.Insert(addUser);
        var result = new JsonResult(new ApiDataResult<SysUser>()
        {
            Code = 200,
            Success = true,
            Message = "用户添加",
        });

        return await Task.FromResult(result);
    }

}